<?php
class DigCms_UtilController extends Zend_Controller_Action
{
    public function indexAction() {
        $this->view->stringReplaceForm = new DigCms_Form_StringReplace();
    }
    
    public function globalStringReplaceAction() {
        $form = new DigCms_Form_StringReplace();
        if($form->validatePost()) {
            $string = $form->getValue('string');
            $replacement = $form->getValue('replacement');
            $contentModel = Dig_Service_Factory::model('item_nodes');
            $affectedNodes = array();
            foreach ($contentModel->fetchAll() as $node) {
                if(strpos($node->content, $string)) {
                    $affectedNodes[] = $node;
                    $node->content = str_replace($string, $replacement, $node->content);
                    $node->save();
                }
            }
            $this->_helper->json(array(
                'message'   => count($affectedNodes) . ' nodes were updated',
                'type'      => 'info'
            ));
        }
        $this->_helper->json(array(
                'message'   => 'Invalid search request',
                'type'      => 'critical'
            ));
    }

    public function xhtmlAction() {
        $this->_helper->layout->disableLayout();
        $dirty = $this->getRequest()->getParam('html', null);
        $xml = @simplexml_load_string($dirty);
        if($xml instanceof SimpleXMLElement) {
            $this->view->xhtml = $xml->asXML();
        }
    }
}
?>
